package com.example.java.designpattern;

import com.example.java.designpattern.factory.abstractfactory.AbstractFactory;
import com.example.java.designpattern.factory.abstractfactory.BFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MyContext {

    private static final Map<String, Object> CONTEXT = new ConcurrentHashMap<>();

    private static final Map<String, String> PROPERTIES = new ConcurrentHashMap<>();

    static {
        CONTEXT.put(AbstractFactory.class.getName(), new BFactory());
    }

    static {
        PROPERTIES.put("1", "1");
    }

    public static <T> T getBean(Class<T> clazz){
        Object o = CONTEXT.get(clazz.getName());
        if(o != null){
            return clazz.cast(o);
        }
        throw new RuntimeException("不存在！");
    }

    public static String getProperty(String key){
        return PROPERTIES.get(key);
    }
}
